package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemCompanyAreaBinding;
import com.example.yichuangyun.model.company.AreaModel;

@SuppressLint("NotifyDataSetChanged")
public class LocationAdapter extends BaseQuickAdapter<AreaModel, DataBindingHolder<ItemCompanyAreaBinding>> {

    private final Context context;
    private int flag;
    private OnItemClickListener onItemClickListener;

    public LocationAdapter(Context context) {
        this.context = context;
    }

    public LocationAdapter(Context context, int flag, OnItemClickListener onItemClickListener) {
        this.context = context;
        this.flag = flag;
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemCompanyAreaBinding> itemCompanyAreaBindingDataBindingHolder, int i, @Nullable AreaModel areaModel) {
        ItemCompanyAreaBinding binding = itemCompanyAreaBindingDataBindingHolder.getBinding();
        if (areaModel != null) {
            binding.tvItem.setText(areaModel.getAdminName());
            binding.llItem.setBackgroundResource(areaModel.isSelect() ? R.drawable.shape_label_bg_sel : R.drawable.shape_label_bg_def);
            binding.tvItem.setTextColor(areaModel.isSelect() ? context.getColor(R.color.color_426FFF) : context.getColor(R.color.color_4A4A4A));
            binding.tvItem.setOnClickListener(view -> onItemClickListener.onItemClick(flag, areaModel));
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemCompanyAreaBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_company_area, viewGroup);
    }

    public interface OnItemClickListener {
        void onItemClick(int flag, AreaModel areaModel);
    }
}
